﻿using System;
using System.Windows;
using System.Windows.Controls;
using mal.WP7.GoogleAnalytics.Business.Resources;
using mal.WP7.GoogleAnalytics.ViewModels;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
//using mal.WP7.GoogleAnalytics.Business.Resources;

namespace mal.WP7.GoogleAnalytics
{
    public partial class EditAccountPage : PhoneApplicationPage
    {
        #region Fields
        ////private const int SaveButtonIndex = 0;
        private const int DeleteButtonIndex = 0;
        ////private const int CloseButtonIndex = 2;

        private EditAccountViewModel viewModel;
        #endregion

        #region Construction / Destruction
        public EditAccountPage()
        {
            InitializeComponent();

            this.Loaded += new System.Windows.RoutedEventHandler(this.OnEditAccountPageLoaded);

            this.viewModel = new EditAccountViewModel();
            this.DataContext = this.viewModel;
        }
        #endregion

        #region enum, struct, class

        #endregion

        #region Properties

        #endregion

        #region Methods
        private void NavigateToConfigurationPage()
        {
            this.NavigationService.Navigate(new Uri("/ConfigurationPage.xaml", UriKind.Relative));
        }
        #endregion

        #region Events
        private void OnEditAccountPageLoaded(object sender, System.Windows.RoutedEventArgs e)
        {
            string login = this.NavigationContext.QueryString["login"];

            this.viewModel.LoadAccount(login);

            ////((ApplicationBarIconButton)this.ApplicationBar.Buttons[SaveButtonIndex]).Text = ResourceText.AccountPageSaveButton;
            ((ApplicationBarIconButton)this.ApplicationBar.Buttons[DeleteButtonIndex]).Text = ResourceText.AccountPageDeleteButton;
            ////((ApplicationBarIconButton)this.ApplicationBar.Buttons[CloseButtonIndex]).Text = ResourceText.AccountPageCloseButton;
        }

        ////private void OnSaveClick(object sender, System.EventArgs e)
        ////{
        ////    this.viewModel.Save();
        ////    this.NavigateToConfigurationPage();
        ////}

        private void OnDeleteClick(object sender, System.EventArgs e)
        {
            MessageBoxResult result = MessageBox.Show(ResourceText.RemoveAccountMessage
                , ResourceText.RemoveAccountCaption
                , MessageBoxButton.OKCancel);

            if (result == MessageBoxResult.OK)
            {
                this.viewModel.Remove();
                this.NavigateToConfigurationPage();
            }
        }

        ////private void OnCloseClick(object sender, System.EventArgs e)
        ////{
        ////    this.NavigateToConfigurationPage();
        ////}

        private void TextBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (sender is TextBox)
            {
                TextBox text = (TextBox)sender;
                text.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            }
            else if (sender is PasswordBox)
            {
                PasswordBox text = (PasswordBox)sender;
                text.GetBindingExpression(PasswordBox.PasswordProperty).UpdateSource();
            }
        }
        #endregion
    }
}